package com.extension.company.common.widget.materialcircle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.extension.company.admin.R;

/**
 * laoding view 圆形加载动画
 */
public class UniversalLoadingView extends ViewGroup {

	private String LOADING_AlERT = "请稍后...";

	private MaterialCircleView materialCircleView;
	private TextView mTipTextView;
	private LinearLayout mLayout;
	private int sWidth;
	private int sHeight;

	/**
	 * need gradient or not
	 */
	private boolean bGradient;

	private int circleColor;
	private int circleWidth;
	private int radius;

	private boolean bTransparent;
	private int alpha;

	private State mLoadState = State.LOADING;

	private Handler mHandler;

	private ReloadListner mReloadListener;

	public boolean isbTransparent() {
		return bTransparent;
	}

	public void setbTransparent(boolean bTransparent) {
		this.bTransparent = bTransparent;
	}

	public enum State {
		GONE,
		LOADING,
		LOADING_FALIED,
		LOADING_EMPTY
	}

	public UniversalLoadingView(Context context) {
		this(context, null);
	}

	public UniversalLoadingView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public UniversalLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		TypedArray t = null;
		try {
			t = context.obtainStyledAttributes(attrs, R.styleable.UniversalLoadingView,
					0, defStyleAttr);
			setbTransparent(t.getBoolean(R.styleable.UniversalLoadingView_bg_transparent, false));
			alpha = t.getDimensionPixelSize(R.styleable.UniversalLoadingView_bg_alpha,
					255);
			LOADING_AlERT = t.getString(R.styleable.UniversalLoadingView_loading_Alert);
			bGradient = t.getBoolean(R.styleable.UniversalLoadingView_bGradient, true);
			circleColor = t.getColor(R.styleable.UniversalLoadingView_circleColor,
					getResources().getColor(android.R.color.holo_blue_light));
			circleWidth = t.getDimensionPixelSize(R.styleable.UniversalLoadingView_circleWidth,
					10);
			radius = t.getDimensionPixelSize(R.styleable.UniversalLoadingView_radius,
					MaterialCircleView.dpToPx(context, 36));
		} finally {
			if (t != null) {
				t.recycle();
			}
		}

		materialCircleView = new MaterialCircleView(context, attrs, defStyleAttr);
		materialCircleView.setbGradient(bGradient); //圆圈颜色是否渐变
		materialCircleView.setCircleColor(circleColor);
		addView(materialCircleView);

		mTipTextView = new TextView(context);
		mTipTextView.setText(LOADING_AlERT);
		mTipTextView.setTextSize(16f);
		mTipTextView.setGravity(Gravity.CENTER);
		mTipTextView.setSingleLine(false);
		mTipTextView.setMaxLines(2);
		mTipTextView.setTextColor(getResources().getColor(android.R.color.darker_gray));
		addView(mTipTextView);

		this.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mLoadState == State.LOADING_EMPTY || mLoadState == State.LOADING_FALIED) {
					if (mReloadListener != null) {
						mReloadListener.reload();
					}
				}
			}
		});

		mHandler = new Handler();

		if (isbTransparent()) {
			setBackgroundColor(getResources().getColor(android.R.color.transparent));
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		measureChildren(widthMeasureSpec, heightMeasureSpec);
		LayoutParams params = (LayoutParams) materialCircleView.getLayoutParams();
		sWidth = MeasureSpec.getSize(widthMeasureSpec);
		sHeight = MeasureSpec.getSize(heightMeasureSpec);
		params.left = (sWidth - radius) / 2;
//        params.top = (sHeight - radius) / 2 ;//位于中间
		params.top = (sHeight - radius) / 4;//位于中间偏上
		params.width = radius;
		params.height = radius;

		LayoutParams tipParams = (LayoutParams) mTipTextView.getLayoutParams();
		int tipWidth = MaterialCircleView.dpToPx(getContext(), 100);
		int tipHeight = MaterialCircleView.dpToPx(getContext(), 50);
		tipParams.left = (sWidth - tipWidth) / 2;
		tipParams.top = (sHeight - radius) / 2 + radius / 2;
		tipParams.width = tipWidth;
		tipParams.height = tipHeight;

		setMeasuredDimension(sWidth, sHeight);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		LayoutParams params = (LayoutParams) materialCircleView.getLayoutParams();
		materialCircleView.layout(params.left, params.top, params.left + params.width
				, params.top + params.height);
		LayoutParams tipParams = (LayoutParams) mTipTextView.getLayoutParams();
		mTipTextView.layout(tipParams.left, tipParams.top, tipParams.left + tipParams.width,
				tipParams.top + tipParams.height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	public void setTransparent(boolean transparent) {
		this.bTransparent = transparent;
		requestLayout();
		invalidate();
	}

	public boolean getIsTransparent() {
		return this.bTransparent;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return super.onInterceptTouchEvent(ev);
		return true;
	}

	@Override
	public ViewGroup.LayoutParams generateLayoutParams(
			AttributeSet attrs) {
		return new LayoutParams(getContext(), attrs);
	}

	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(
			ViewGroup.LayoutParams p) {
		return new LayoutParams(p);
	}

	@Override
	protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
		return p instanceof LayoutParams;
	}

	private static class LayoutParams extends ViewGroup.LayoutParams {

		public int left = 0;
		public int top = 0;

		public LayoutParams(Context c, AttributeSet attrs) {
			super(c, attrs);
		}

		public LayoutParams(int width, int height) {
			super(width, height);
		}

		public LayoutParams(ViewGroup.LayoutParams source) {
			super(source);
		}
	}

	public void setOnReloadListener(ReloadListner listener) {
		this.mReloadListener = listener;
	}

	/**
	 * reload interface
	 */
	public interface ReloadListner {
		public void reload();
	}
}
